package com.hunttown.mes.manage.service;

import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.rpc.api.TransportLoginWxRpcService;
import com.hunttown.mes.rpc.domain.TransportLoginWxDTO;
import com.hunttown.mes.rpc.domain.query.TransportLoginWxDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * created by wangjunfu on 2019-05-19 01:51:47
 */
@Service
public class TransportLoginWxManageService {

    private final static Logger logger = LoggerFactory.getLogger(TransportLoginWxManageService.class);

    private final TransportLoginWxRpcService rpcService;

    @Autowired
    public TransportLoginWxManageService(TransportLoginWxRpcService rpcService) {
        this.rpcService = rpcService;
    }

    // 新增
    public TransportLoginWxDTO insert(TransportLoginWxDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(TransportLoginWxDTO objDTO) {
        return rpcService.updateInfoById(objDTO);
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(TransportLoginWxDTOQuery objDTOQuery) {
        return rpcService.updateInfoByQuery(objDTOQuery);
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        return rpcService.deleteById(id);
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        TransportLoginWxDTOQuery query = new TransportLoginWxDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public TransportLoginWxDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public TransportLoginWxDTO getByQuery(TransportLoginWxDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<TransportLoginWxDTO> getForPage(TransportLoginWxDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    // 改变某条记录的使用状态
    public Boolean changeState(Integer id, Integer state) {
        TransportLoginWxDTO obj = new TransportLoginWxDTO();
        obj.setStopFlag(state);
        obj.setId(id);
        return updateInfoById(obj);
    }

    //根据手机号获取
    public TransportLoginWxDTO getByByMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return null;
        }

        TransportLoginWxDTOQuery query = new TransportLoginWxDTOQuery();
        query.setMobile(mobile);
        query.setOrderBy("order by id desc");
        return getByQuery(query);
    }

    //根据UnionId获取
    public TransportLoginWxDTO getByByUnionId(String unionId) {
        if (StringUtils.isBlank(unionId)) {
            return null;
        }

        TransportLoginWxDTOQuery query = new TransportLoginWxDTOQuery();
        query.setUnionId(unionId);
        query.setOrderBy("order by id desc");
        return getByQuery(query);

    }

    /**
     * 根据用户user_id获取用户微信open_id
     *
     * @param userId 用户Id
     * @return openid
     */
    public String getWxOpenIdByUserId(Integer userId) {
        return rpcService.getWxOpenIdByUserId(userId);
    }
}